package cj.web.admin.controllers;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.DateUtils;
import cj.web.admin.constants.KDAddressAreaEnum;
import cj.web.admin.constants.KDAddressStreetEnum;
import cj.web.admin.constants.KDBookingStatusEnum;
import cj.web.admin.constants.KDInTypeEnum;
import cj.web.admin.constants.MemuEnum;
import cj.web.admin.dao.KDBookingCardDAO;
import cj.web.admin.domain.KDBookingCard;
import cj.web.admin.domain.KDBookingPaper;
import cj.web.login.controllers.BaseController;

@Path("/kdbookingcard")
public class KDBookingCardController extends BaseController{

	private static Logger logger = Logger.getLogger(KDBookingCardController.class.getName());
	
	@Autowired
	private KDBookingCardDAO kdBookingCardDao;
	
	@Get("show")
	public String show(Model model,KDBookingCard kdBookingCard) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.kdgsdbkqy);
        
		EnumSet<KDBookingStatusEnum> kdBookingStatusEnum = EnumSet.allOf(KDBookingStatusEnum.class);
        model.add("kdBookingStatusEnum",kdBookingStatusEnum);
        
        model.add("kdAddressAreaEnum", EnumSet.allOf(KDAddressAreaEnum.class));
		model.add("kdAddressStreetEnum", EnumSet.allOf(KDAddressStreetEnum.class));
		model.add("currentDate", DateUtils.getNowTime(DateUtils.DATE_SMALL_STR));
        model.add("kdInTypeEnum",EnumSet.allOf(KDInTypeEnum.class));
        return "portal/kdBookingCardList.vm";
	}

	@Post("getList")
	public String queryList(KDBookingCard kdBookingCard,@Param("areaKey") String areaKey,@Param("streetKey") String streetKey) {
	
		JSONObject jo = new JSONObject();
		
		if(areaKey!=null && !"".equals(areaKey))
		{
			kdBookingCard.setArea(KDAddressAreaEnum.getValue(areaKey).getValue());
		}
		
		if(streetKey!=null && !"".equals(streetKey))
		{
			kdBookingCard.setStreet(KDAddressStreetEnum.getValue(streetKey).getValue());
		}
		
		List<Map> objs = kdBookingCardDao.query(kdBookingCard);
		
		for(Map obj:objs)
		{
			obj.put("beginAt", DateUtils.getMinusTime(obj.get("beginAt").toString(), DateUtils.DATE_SMALL_STR, 0));
			obj.put("createAT", DateUtils.getMinusTime(obj.get("createAT").toString(), DateUtils.DATE_FULL_STR, 0));
			obj.put("address", cityWuhan+KDAddressAreaEnum.getName(Integer.parseInt(obj.get("area").toString()))+
					KDAddressStreetEnum.getName(Integer.parseInt(obj.get("street").toString()))+obj.get("address").toString());
		}
		
		int size = kdBookingCardDao.querySzie(kdBookingCard);
		
		jo.put("data", objs);
		jo.put("draw", kdBookingCard.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		//MemuEnum
        model.add("MemuEnum",MemuEnum.kdgsdbkqy);
        
        KDBookingCard obj = kdBookingCardDao.queryById(id);
		model.add("obj", obj);
		
		return "portal/kdBookingCardEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,KDBookingCard kdBookingCard,HttpServletRequest request)
	{
		kdBookingCardDao.updateMemoById(kdBookingCard.getId(), kdBookingCard.getMemo());
		return "r:show";
	}
	
	@Get("updateStatus")
	public String updateStatus(Model model,@Param("id") int id)
	{
		kdBookingCardDao.updateStatusById(id);
		return "r:show";
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id)
	{
		kdBookingCardDao.deleteById(id);
		return "r:show";
	}
}
